package cn.com.server.common;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import com.alibaba.fastjson.JSON;

import cn.com.server.common.util.SpringUtil;
import cn.com.server.common.util.YamlUtils;

public class AjaxCodeUtil {
	
	private static Map<String,AjaxObject> code = new LinkedHashMap<>();
	
	public static AjaxObject getAjaxObject(String key) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		HttpSession session = request.getSession();
		try {
			if(session!=null) {
				@SuppressWarnings("unchecked")
				Map<String,AjaxObject> ajaxobjectSessionCode = (Map<String, AjaxObject>) session.getAttribute("AJAXOBJECT_SESSION_CODE");
				if(ajaxobjectSessionCode!=null) {
					AjaxObject ajaxObject = ajaxobjectSessionCode.get(key);
					if(ajaxObject==null) {
						ajaxObject = new AjaxObject(-1,"AjaxObjectException");
					}
					return new AjaxObject(ajaxObject.getCode(),ajaxObject.getMsg());
				}else {
					session.setAttribute("AJAXOBJECT_SESSION_CODE",code);
					AjaxObject ajaxObject = code.get(key);
					if(ajaxObject==null) {
						ajaxObject = new AjaxObject(-1,"AjaxObjectException");
					}
					return new AjaxObject(ajaxObject.getCode(),ajaxObject.getMsg());
				}
				
			}else {
				AjaxObject ajaxObject = code.get(key);
				if(ajaxObject==null) {
					ajaxObject = new AjaxObject(-1,"AjaxObjectException");
				}
				return new AjaxObject(ajaxObject.getCode(),ajaxObject.getMsg());
			}
		}catch (Exception e) {
			e.printStackTrace();
			AjaxObject ajaxObject = code.get(key);
			if(ajaxObject==null) {
				ajaxObject = new AjaxObject(-1,"AjaxObjectException");
			}
			return new AjaxObject(ajaxObject.getCode(),ajaxObject.getMsg());
		}
		
	}
	
	public static AjaxObject onChangeCode(String language,HttpSession session) {
		try {
			Map<String, Object> map = YamlUtils.getYamlSource("ajaxcode_"+language+".yml");
			Map<String,AjaxObject> ajaxobjectSessionCode = new LinkedHashMap<>();
			Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator(); 
			while (entries.hasNext()) { 
			  Map.Entry<String, Object> entry = entries.next(); 
			  ajaxobjectSessionCode.put(entry.getKey(), JSON.parseObject(JSON.toJSONString(entry.getValue()),AjaxObject.class));
			}
			if(ajaxobjectSessionCode.size()>0) {
				session.setAttribute("AJAXOBJECT_SESSION_CODE",ajaxobjectSessionCode);
				return new AjaxObject(-2,"AjaxobjectSessionCodeIsNullException");
			}else {
				session.setAttribute("AJAXOBJECT_SESSION_CODE",code);
				return getAjaxObject("success");
			}
		}catch(Exception e) {
			e.printStackTrace();
			session.setAttribute("AJAXOBJECT_SESSION_CODE",code);
			return new AjaxObject(-3,"OnChangeCodeException");
		}
	}
	
//	public static AjaxObject getAjaxObject(String key) {
//		AjaxObject ajaxObject = code.get(key);
//		if(ajaxObject==null) {
//			ajaxObject = new AjaxObject(-1,"AjaxObjectException");
//		}
//		return ajaxObject;
//	}
	
	static {
		try {
			Map<String, Object> map = YamlUtils.getYamlSource("ajaxcode.yml");
			Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator(); 
			while (entries.hasNext()) { 
			  Map.Entry<String, Object> entry = entries.next(); 
			  code.put(entry.getKey(), JSON.parseObject(JSON.toJSONString(entry.getValue()),AjaxObject.class));
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static String getMessage(String code) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		Locale locale = (Locale) request.getSession(true).getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
		//messageSource
		ResourceBundleMessageSource messageSource = (ResourceBundleMessageSource) SpringUtil.getBean("messageSource");
		return messageSource.getMessage(code, null, locale);
	}
}
